/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.madev.isellcrazyitems;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.madev.isellcrazyitems.entity.User;
import com.madev.isellcrazyitems.handler.DatabaseHandler;
import com.madev.sales.R;


public class LoginActivity extends Activity {
	Button btnLogin;
	Button btnLinkToRegister;
	EditText inputEmail;
	EditText inputPassword, registerAddress;
	TextView loginErrorMsg;

	// JSON Response node names
	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR = "error";
	private static String KEY_ERROR_MSG = "error_msg";
	private static String KEY_UID = "uid";
	private static String KEY_UNIQUEID = "uniqueID";
	private static String KEY_NAME = "name";
	private static String KEY_EMAIL = "email";
	
	private static String KEY_ADDRESS = "address";
	private static String KEY_CREATEDATE = "createDate";
	private static String KEY_UPDATEDATE = "updateDate";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// Importing all assets like buttons, text fields
		inputEmail = (EditText) findViewById(R.id.loginEmail);
		inputPassword = (EditText) findViewById(R.id.loginPassword);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
		loginErrorMsg = (TextView) findViewById(R.id.login_error);
		// Login button Click Event
		btnLogin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				String email = inputEmail.getText().toString();
				String password = inputPassword.getText().toString();
				DatabaseHandler ldb = new DatabaseHandler(getApplicationContext());
				DatabaseHandler  udb = new DatabaseHandler (getApplicationContext());
				Log.d("Button", "Login");

				// check for login response
				
				loginErrorMsg.setText("");
				
				if(!email.equals("") && !password.equals(""))
				{
					User u = udb.getUserByEmailAndPassword(email, password);
					if (u != null)
					{
						// user successfully logged in
						// Store user details in SQLite Database
	
						
						// Clear all previous data in database
						ldb.logoutUser();
						ldb.addLogin(u);						
						
						// Launch Dashboard Screen
						Intent dashboard;
						if (u.getUserType().equals("customer"))
						{
							dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
						}
						else
						{
							dashboard = new Intent(getApplicationContext(), MainActivity.class);
						}
						
						// Close all views before launching Dashboard
						dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(dashboard);
						
						// Close Login Screen
						finish();
					}else{
						// Error in login
						loginErrorMsg.setText("Incorrect username/password");
					}
				}
				else 
				{
					loginErrorMsg.setText("Please enter your username and password");
				}
				
			}
		});

		// Link to Register Screen
		btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(),
						RegisterActivity.class);
				startActivity(i);
				finish();
			}
		});
	}
}
